import Vue from 'vue'
//1.下载  yarn add vue-router
//2.引入vue-router
import VueRouter from "vue-router";
import { getToken } from '../utils/localData';


//3.使用路由
Vue.use(VueRouter)
//4.路由隐射关系
// 4.1 引入路由组件页面
import Login from "../views/login.vue";
import Register from "../views/Register.vue";
import User from "../views/User.vue";
import UserEdit from "../views/UserEdit.vue";
import Home from "../views/index.vue";
import Article from "../views/Article.vue";
import UserFollow from "../views/UserFollow.vue";
import UserCollect from "../views/UserCollect.vue";
import UserComment from "../views/UserComment.vue";
import Comment from "../views/Comment.vue";
import Category from "../views/Category.vue";
import Search from "../views/Search.vue";

const routes = [
    { path: '/', component: Home }
    , {
        name: 'Login',
        path: '/login',
        component: Login,
    },
    { path: '/register', component: Register },
    { path: '/Comment/:id', component: Comment },
    { path: '/category', component: Category },
    { path: '/search', component: Search },
    { path: '/article/:id', component: Article },
    { meta: { check: true }, path: '/user', component: User },
    // 🧐直接在配置路由信息的时候，写上需要检查哪些页面
    { meta: { check: true }, path: '/User_edit', component: UserEdit },
    { meta: { check: true }, path: '/userFollow', component: UserFollow },
    { meta: { check: true }, path: '/user_collect', component: UserCollect },
    { meta: { check: true }, path: '/user_comment', component: UserComment },

]
//5.创建路由模块
const router = new VueRouter({
    //使用routes进行配置路由
    routes,
    mode: 'history'
})

//路由守卫
//to 目标路由对象地址
// from  跳转前的对象地址
// next  是否放行的函数
router.beforeEach((to, from, next) => {
    //要校验token的页面
    // const pathArr = ['/user', '/User_edit']
    // console.log(to);
    // 🧐判断是否为要检查的页面
    if (to.meta.check) {
        //如果拿到token
        if (getToken()) {
            //放行
            next()
        } else {
            // 没有token,返回登录页面
            next({ path: '/login' })
        }
    } else {
        // 普通页面直接放行
        next()
    }
})

// 6.1暴露
export default router